/**
 * 船票系统配置文件
 * 包含测试数据、线路信息等
 */

// 城市列表
const cities = ['香洲港', '九洲港', '桂山岛', '东澳岛', '外伶仃岛', '大万山岛'];

// 生成未来5天的日期
function generateDateList() {
  const dateList = [];
  const today = new Date();
  
  for (let i = 0; i < 5; i++) {
    const date = new Date();
    date.setDate(today.getDate() + i);
    
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    
    dateList.push(`${year}-${month}-${day}`);
  }
  
  return dateList;
}

const dateList = generateDateList();

// 生成随机时间
function generateRandomTime(startHour, startMinute, endHour, endMinute) {
  const hour = Math.floor(Math.random() * (endHour - startHour + 1)) + startHour;
  const minute = Math.floor(Math.random() * (endMinute - startMinute + 1)) + startMinute;
  
  return `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`;
}

// 生成随机票价
function generateRandomPrice(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 生成随机座位数
function generateRandomSeats(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 计算路线总时长
function calculateDuration(departureTime, arrivalTime) {
  const [depHour, depMinute] = departureTime.split(':').map(Number);
  const [arrHour, arrMinute] = arrivalTime.split(':').map(Number);
  
  let hourDiff = arrHour - depHour;
  let minuteDiff = arrMinute - depMinute;
  
  if (minuteDiff < 0) {
    hourDiff--;
    minuteDiff += 60;
  }
  
  if (hourDiff < 0) {
    hourDiff += 24;
  }
  
  return `约${hourDiff}小时${minuteDiff > 0 ? minuteDiff + '分钟' : ''}`;
}

// 生成线路数据
function generateRouteData() {
  const routeData = {};
  
  // 生成每天的路线数据
  dateList.forEach(date => {
    routeData[date] = {};
    
    // 为每对城市生成路线
    for (let i = 0; i < cities.length; i++) {
      for (let j = 0; j < cities.length; j++) {
        if (i !== j) { // 确保出发地和目的地不同
          const fromCity = cities[i];
          const toCity = cities[j];
          const routeKey = `${fromCity}-${toCity}`;
          
          // 生成2-5条船票数据
          const ticketCount = Math.floor(Math.random() * 4) + 2;
          const tickets = [];
          
          for (let k = 0; k < ticketCount; k++) {
            // 早上、下午、晚上的船票
            let startHour, endHour;
            if (k === 0) {
              startHour = 7;
              endHour = 10;
            } else if (k === 1) {
              startHour = 12;
              endHour = 15;
            } else {
              startHour = 16;
              endHour = 19;
            }
            
            const departureTime = generateRandomTime(startHour, 0, endHour, 30);
            
            // 计算到达时间（假设1-3小时后到达）
            const tripHours = Math.floor(Math.random() * 3) + 1;
            const [depHour, depMinute] = departureTime.split(':').map(Number);
            const arrHour = (depHour + tripHours) % 24;
            const arrivalTime = `${String(arrHour).padStart(2, '0')}:${String(depMinute).padStart(2, '0')}`;
            
            const duration = calculateDuration(departureTime, arrivalTime);
            const basePrice = generateRandomPrice(30, 200);
            
            tickets.push({
              id: `${date}-${fromCity}-${toCity}-${k + 1}`,
              departureTime,
              departurePlace: fromCity,
              arrivalTime,
              arrivalPlace: toCity,
              duration,
              price: basePrice,
              regularSeats: generateRandomSeats(0, 30),
              vipSeats: generateRandomSeats(0, 15),
              luxurySeats: generateRandomSeats(0, 10),
              totalSeats: generateRandomSeats(30, 100),
              routeId: `${fromCity}-${toCity}`
            });
          }
          
          // 按出发时间排序
          tickets.sort((a, b) => {
            const timeA = a.departureTime.split(':').map(Number);
            const timeB = b.departureTime.split(':').map(Number);
            
            if (timeA[0] !== timeB[0]) {
              return timeA[0] - timeB[0];
            }
            return timeA[1] - timeB[1];
          });
          
          routeData[date][routeKey] = tickets;
        }
      }
    }
  });
  
  return routeData;
}

// 生成所有路线数据
const routeData = generateRouteData();

// 获取特定日期和路线的船票数据
function getTickets(date, fromCity, toCity) {
  const routeKey = `${fromCity}-${toCity}`;
  
  // 如果没有特定日期的数据，返回今天的数据
  if (!dateList.includes(date)) {
    date = dateList[0];
  }
  
  // 如果请求的路线不存在，返回空数组
  if (!routeData[date] || !routeData[date][routeKey]) {
    return [];
  }
  
  return routeData[date][routeKey];
}

// 获取所有船票数据
function getAllTickets() {
  const allTickets = [];
  
  // 遍历所有日期
  Object.keys(routeData).forEach(date => {
    // 遍历每个日期下的所有路线
    Object.keys(routeData[date]).forEach(routeKey => {
      // 添加该路线下的所有船票
      allTickets.push(...routeData[date][routeKey]);
    });
  });
  
  return allTickets;
}

// 导出数据和函数
export default {
  cities,
  dateList,
  getTickets,
  getAllTickets
};
